$(function () {

    var Menu = {
        tree: $('#tb')
    };


    getTrees(1);

    /**
     * 获取数据
     * @type {$ax}
     */
    function getTrees() {
        var ax = new $ax(ctx + "/sys/menu/selectList", function (result) {
            if (result.success) {
                init(result.data);
            }
        });
        ax.start();
    }

    /**
     * 初始化
     * @param data
     */
    var init = function (data) {
        Menu.tree.bootstrapTable({
            data: data,
            idField: 'id',
            uniqueId: 'id',
            toolbar: '#toolbar',
            columns: [
                {
                    field: 'check',
                    checkbox: true
                },
                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'url',
                    title: '菜单地址'
                },
                {
                    field: 'isMenu',
                    title: '是否菜单',
                    formatter: function (value, row, index) {
                        if (value == '1') {
                            return "菜单";
                        }
                        return "按钮"
                    }
                },
                {
                    field: 'sort',
                    title: '排序'
                }, {
                    field: 'level',
                    title: '层级'
                },
            ],
            treeShowField: 'name',
            parentIdField: 'pId',
            onResetView: function (data) {
                Menu.tree.treegrid({
                    initialState: 'collapsed',
                    treeColumn: 1,
                    expanderExpandedClass: 'mdi mdi-folder-open',
                    expanderCollapsedClass: 'mdi mdi-folder',
                    onChange: function () {
                        Menu.tree.bootstrapTable('resetWidth');
                    }
                });
                Menu.tree.treegrid('getRootNodes').treegrid('expand');
            },
        });
    };

    /**
     * 添加页面
     */
    $("#add").click(function () {
        open(ctx + "/sys/menu/add", "添加菜单", function () {
            destory(Menu.tree);
            getTrees();
        }, false);
    });

    $("#edit").click(function () {
        if (check(Menu.tree)) {
            var row = getRow(Menu.tree);
            open(ctx + "/sys/menu/edit?id=" + row.id, "添加菜单", function () {
                destory(Menu.tree);
                getTrees();
            }, false);
        }
    });

    /**
     * 删除按钮
     */
    $("#delete").click(function () {
        if (check(Menu.tree)) {
            var row = getRow(Menu.tree);
            confirm("确定删除该菜单吗?", function () {
                var ax = new $ax(ctx + "/sys/menu/delete/" + row.id, function (result) {
                    if (result.success) {
                        success("删除成功");
                        destory(Menu.tree);
                        getTrees();
                    }
                });
                ax.start();
            });
        }
    });

    /**
     * 刷新
     */
    $("#refresh").click(function () {
        destory(Menu.tree);
        getTrees();
    });
});